<?php

namespace app\common\model;

use fast\Random;
use think\Model;


class Withdrawal extends Model
{

    

    

    // 表名
    protected $name = 'withdrawal';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_data_text',
        'source_data_text',
        'status_text',
        'operate_time_text'
    ];
    

    
    public function getSourceDataList()
    {
        return ['1' => __('Source_data 1'), '2' => __('Source_data 2'), '3' => __('Source_data 3'), '4' => __('Source_data 4')];
    }
    public function getTypeDataList()
    {
        return ['0' => __('Type_data 0'), '1' => __('Type_data 1'), '2' => __('Type_data 2')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2')];
    }


    public function getTypeDataTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type_data']) ? $data['type_data'] : '');
        $list = $this->getTypeDataList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    public function getSourceDataTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['source_data']) ? $data['source_data'] : '');
        $list = $this->getSourceDataList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getOperateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['operate_time']) ? $data['operate_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setOperateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function Merchant()
    {
        return $this->belongsTo('Merchant', 'store_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function Regionservice()
    {
        return $this->belongsTo('Regionservice', 'store_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function Dealer()
    {
        return $this->belongsTo('app\common\model\card\Dealer', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function Distributor()
    {
        return $this->belongsTo('Distributor', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    /**
     * 生成兑换号码
     * */
    public function getTrans_no($num)
    {
        $code = Random::numeric($num);
        $log = \app\common\model\Withdrawal::where(["trans_no" => $code])->find();
        if (!empty($log)) {
            $this->getTrans_no();
        }
        return $code;
    }
}
